using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using log4net;
namespace Bamboo.Core.Common.Logger
{
    public abstract class BambooLogger
    {
        private static ILog log = null;
        protected abstract string FormatExceptionContent(Exception ex);
        protected abstract string FormatMessage(object message);
        public BambooLogger(string loggerName)
        {
            //Initialize log4net
            log4net.Config.XmlConfigurator.Configure();
            log = log4net.LogManager.GetLogger(loggerName);
        }
        protected string GetMessage(object exceptionObject)
        {
            string message = "";
            if (exceptionObject is string)
            {
                message = FormatMessage(exceptionObject);
            }
            if (exceptionObject is Exception)
            {
                message = FormatExceptionContent((Exception)exceptionObject);
            }
            return message;
        }
        public void Debug(object exceptionObject)
        {
            log.Debug(GetMessage(exceptionObject));
        }

        public void Error(object exceptionObject)
        {
            log.Error(GetMessage(exceptionObject));
        }

        public void Info(object exceptionObject)
        {
            log.Info(GetMessage(exceptionObject));
        }

        public void Warn(object exceptionObject)
        {
            log.Warn(GetMessage(exceptionObject));
        }

    }

    public class BusinessLog : BambooLogger
    {
        public BusinessLog()
            : base("BusinessLog")
        {
        }
        protected override string FormatExceptionContent(Exception ex)
        {
            return string.Format("Error message: {0}. \r\n Inner exception: {1}.\r\n Stack trace {2}", ex.Message, ex.InnerException, ex.StackTrace);
        }
        
        protected override string FormatMessage(object message)
        {
            return (string) message;
        }

    }

    public class WebLog : BambooLogger
    {
        public WebLog()
            : base("WebLog")
        {
        }
        /// <summary>
        /// Format a log exceptionObject
        /// </summary>
        /// <param name="exceptionObject"></param>
        /// <returns></returns>
        protected override string FormatMessage(object message)
        {
            string newMessage = "";

            string sessionID = "";
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
            {
                sessionID = HttpContext.Current.Session.SessionID;
            }
            if (message is Exception)
            {
                Exception ex = (Exception)message;
                newMessage = string.Format("[SessionID={0}] {1}\n Inner exception: {2}", new object[] { sessionID, ex.StackTrace, ex.InnerException });
            }
            else
            {
                newMessage = string.Format("[SessionID={0}] {1}", new object[] { sessionID, message });
            }
            return newMessage;
        }

        protected override string FormatExceptionContent(Exception ex)
        {
            string newMessage = "";
            if (HttpContext.Current != null)
            {
                newMessage = string.Format("[SessionID={0}] Exception Message {1}\r\n Stack trace {2}\r\nInner Exception {3}", new object[] { HttpContext.Current.Session.SessionID, ex.Message, ex.StackTrace, ex.InnerException });
            }
            else
            {
                newMessage = ex.Message;
            }
            return newMessage;
        }
    }

}
